/**
 * 获取URL路径和URL查询字符串
 */

//导入 http 模块
const http = require('http');
//导入 url 模块
const url = require('url');


//创建服务对象 create 创建 server 服务
const server = http.createServer((request,response) => {

    console.log("request.url",request.url);
    // /username=admin&password=123456

    // url.parse 对url进行解析,若第二个参数为true,则会将URL查询字符串转换为对象,这样方便操作
    let res = url.parse(request.url,true);
    console.log("res",res);
    /**
     *  Url {
          protocol: null,
          slashes: null,
          auth: null,
          host: null,
          port: null,
          hostname: null,
          hash: null,
          search: '?username=admin&password=123456',
          query: [Object: null prototype] { username: 'admin', password: '123456' },
          pathname: '/search',
          path: '/search?username=admin&password=123456',
          href: '/search?username=admin&password=123456'
        }
     */
    // 获取URL路径
    console.log("res.pathname",res.pathname); // /search
    // 获取URL查询字符串
    console.log(res.query.username); //admin
    console.log(res.query.password); //123456




    // 解决乱码
    response.setHeader('content-type','text/html;charset=utf-8');
    // 设置响应体并结束响应
    response.end("您好");

});

// 监听端口9000,第二个参数只有服务启动成功之后才会执行
server.listen(9000,() => {
    console.log("服务已经启动, 端口 9000 监听中");
})
































